package com.wyl.mine.message

import android.content.Intent
import android.view.View
import androidx.databinding.ObservableInt
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.MessagePage
import com.wyl.base.bean.MsgData
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.utils.KLog
import com.wyl.mine.R
import com.wyl.mine.databinding.MessageActivityBinding
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel


@Route(path = MessagePage)
class MessageActivity : BindingActivity<MessageActivityBinding>(), ItemClickPresenter<MsgData> {

    private val viewModel: MessageViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.message_activity

    override fun initView() {
        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                CommonTitleBar.ACTION_RIGHT_TEXT -> {
                    viewModel.clearMessage()
                }
                else -> {
                }
            }
        }

        binding.include.recyclerView.apply {
            layoutManager = mLayoutManager
            adapter = mAdapter
            addItemDecoration(mDivider)
        }
    }

    override fun loadData() {
        viewModel.refresh()
    }

    private val mAdapter by lazy {
        SingleTypeAdapter(
            this,
            R.layout.message_item,
            viewModel.dataSource
        ).apply {
            itemPresenter = this@MessageActivity
        }
    }

    override fun onItemClick(v: View, item: MsgData) {
        startActivity(Intent(this, MessageDetailActivity::class.java).putExtra("mUserData", item))
        viewModel.changeState(item.id)
        item.status = 2
        KLog.d("item.status = ${item.status}")
    }

    private val mLayoutManager by lazy { LinearLayoutManager(this) }

    private val mDivider by lazy {
        RecyclerViewDivider(this, RecyclerViewDivider.HORIZONTAL)
    }


    var topIndex = ObservableInt(0)

    fun checkTopIndex(i: Int) {
        topIndex.set(i)
    }

}
